local skill = fk.CreateSkill {
  name = "lvshi__xuanhuo",
  tags = {"Phase"}
}

Fk:loadTranslationTable{
  ["lvshi__xuanhuo"] = "眩惑",
  [":lvshi__xuanhuo"] = "阶段技，你可视为使用【落井下石】或【暗度陈仓】。己方角色的出牌阶段开始时，其可交给你一张红桃牌，获得“眩惑”直到此阶段结束。",
  ["#lvshi__xuanhuo-prompt"] = "眩惑：你可视为使用【落井下石】或【暗度陈仓】",
  ["#lvshi__xuanhuo_trigger"] = "眩惑",
  ["#lvshi__xuanhuo-give"] = "眩惑：你可以交给 %dest 一张红桃牌",

  ["$lvshi__xuanhuo1"] = "待其倦怠，我举红旗，将军可虎奔而攻。",
  ["$lvshi__xuanhuo2"] = "夏侯兵至，我举白旗，将军便按兵不动。",
}
local ls = require "packages/lvshi/utility"
local U = require "packages/utility/utility"

skill:addEffect("viewas", {
  prompt = "#lvshi__xuanhuo-prompt",
  pattern = ".|.|.|.|lvshi__rockfall,lvshi__secret_advance",
  interaction = function(self)
    local names = {"lvshi__rockfall", "lvshi__secret_advance"}
    return UI.CardNameBox {choices = names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end
})

skill:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name) and target.phase == Player.Play and ls.isFriend(player, target)
    and player ~= target
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(target, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = skill.name,
      pattern = ".|.|heart",
      cancelable = true,
      prompt = "#lvshi__xuanhuo-give::"..player.id
    })
    if #cards > 0 then
      event:setCostData(self, {id = cards[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(event:getCostData(self).id, Card.PlayerHand, player, fk.ReasonPrey, skill.name, "", true, target)
    room:handleAddLoseSkills(target, "lvshi__xuanhuo")
    room.logic:getCurrentEvent():findParent(GameEvent.Phase, true, Player.HistoryGame):addCleaner(function ()
      room:handleAddLoseSkills(target, "-lvshi__xuanhuo")
    end)
  end,
})

return skill
